﻿public static class IconHelper
{
    /// <summary>
    /// 将任意图片文件（PNG/JPG/BMP等）转换为 Icon 对象
    /// </summary>
    /// <param name="imagePath">图片文件路径</param>
    /// <param name="iconSize">图标尺寸（默认32x32）</param>
    /// <returns>Icon对象</returns>
    public static Icon ConvertImageToIcon(string imagePath, int iconSize = 32)
    {
        if (!File.Exists(imagePath))
            throw new FileNotFoundException("图片文件不存在", imagePath);

        using (Bitmap originalBitmap = new Bitmap(imagePath))
        {
            // 缩放到指定尺寸
            using (Bitmap resizedBitmap = new Bitmap(originalBitmap, new Size(iconSize, iconSize)))
            {
                IntPtr hIcon = resizedBitmap.GetHicon();
                return Icon.FromHandle(hIcon);
            }
        }
    }
}
